package com.rem.rabbitmq.ee;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * 抽取连接类
 *
 * @author Rem
 * @date 2021-12-23
 */
public class RabbitMqUtils {
    /**
     * 轮训队列
     */
    public static final String QUEUE_ROTATION = "QUEUE_ROTATION";

    /**
     * ack队列
     */
    public static final String QUEUE_ACK = "QUEUE_ACK";

    /**
     * 持久化队列
     */
    public static final String QUEUE_DURABLE = "QUEUE_DURABLE";

    /**
     * 不公平分发队列
     */
    public static final String QUEUE_UNFAIR = "QUEUE_UNFAIR";

    /**
     * 发布确认队列
     */
    public static final String QUEUE_CONFIRM = "QUEUE_CONFIRM";

    /**
     * 直连交换机
     */
    public static final String EXCHANGE_DIRECT = "EXCHANGE_DIRECT";

    /**
     * 广播交换机
     */
    public static final String EXCHANGE_FANOUT = "EXCHANGE_FANOUT";

    /**
     * 主题交换机
     */
    public static final String EXCHANGE_TOPIC = "EXCHANGE_TOPIC";

    /**
     * 死信队列
     */
    public static final String QUEUE_DEAD = "QUEUE_DEAD";

    /**
     * 死信routingKey
     */
    public static final String ROUTING_KEY_DEAD = "ROUTING_KEY_DEAD";

    /**
     * 死信交换机
     */
    public static final String EXCHANGE_DEAD = "EXCHANGE_DEAD";

    /**
     * 普通队列-TTL
     */
    public static final String QUEUE_NORMAL_TTL = "QUEUE_NORMAL_TTL";
    /**
     * 普通routingKey-TTL
     */
    public static final String ROUTING_KEY_NORMAL_TTL = "ROUTING_KEY_NORMAL_TTL";
    /**
     * 普通交换机-TTL
     */
    public static final String EXCHANGE_NORMAL_TTL = "EXCHANGE_NORMAL_TTL";

    /**
     * 普通队列-TTL
     */
    public static final String QUEUE_NORMAL_MAXLENGTH = "QUEUE_NORMAL_MAXLENGTH";
    /**
     * 普通routingKey-TTL
     */
    public static final String ROUTING_KEY_NORMAL_MAXLENGTH = "ROUTING_KEY_NORMAL_MAXLENGTH";
    /**
     * 普通交换机-TTL
     */
    public static final String EXCHANGE_NORMAL_MAXLENGTH = "EXCHANGE_NORMAL_MAXLENGTH";

    /**
     * 普通队列-TTL
     */
    public static final String QUEUE_NORMAL_REJECT = "QUEUE_NORMAL_REJECT";
    /**
     * 普通routingKey-TTL
     */
    public static final String ROUTING_KEY_NORMAL_REJECT = "ROUTING_KEY_NORMAL_REJECT";
    /**
     * 普通交换机-TTL
     */
    public static final String EXCHANGE_NORMAL_REJECT = "EXCHANGE_NORMAL_REJECT";


    private static Connection connection;
    private static Channel channel;


    private RabbitMqUtils() {
    }


    /**
     * 获取信道
     *
     * @return
     * @throws Exception
     */
    public static Channel getChannel() throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("106.14.72.13");
        factory.setPort(5672);
        factory.setUsername("rem");
        factory.setPassword("123456");
        connection = factory.newConnection();
        channel = connection.createChannel();

        return channel;
    }


    /**
     * 关闭连接
     *
     * @throws Exception
     */
    public static void close() throws Exception {
        if (channel.isOpen()) {
            channel.close();
        }
        if (connection.isOpen()) {
            connection.close();
        }
    }

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        System.err.println(channel.isOpen());
        Connection connection = channel.getConnection();
        System.err.println(connection);
        close();
    }

}
